package common.kingdee.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import common.security.domain.Department;
import common.security.domain.Role;
import common.security.domain.User;
import common.security.domain.repositoryInterface.DepartmentRepository;
import common.security.domain.repositoryInterface.UserRoleRepository;
import common.tools.DeSerializationForConfigurable;

@Configurable
public class KingdeeUser implements Serializable,DeSerializationForConfigurable,User {
	private static final long serialVersionUID = 5468461392646738148L;
	private String departmentlongName;
	@Autowired
	private transient DepartmentRepository departmentRepository;
	private boolean enabled = false;
	private String id;
	private String loginName;
	private String chineseName;
	private String note;
	private String openId;
	private String jobTitle;
	private String imageUrl;
	@Autowired
	private transient UserRoleRepository userRoleRepository;
    private KingdeeUser(){
    	
    }
	public KingdeeUser(String id, String mobile,String chineseName, String note, String departmentlongName,String imageUrl) {
		this.id = id;
		this.loginName = mobile;
		this.chineseName=chineseName;
		this.note = note; 
		this.departmentlongName = departmentlongName;
		this.imageUrl=imageUrl;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Set<Role> roles = getUserRoleRepository().getRolesByUserId(getId());
		Collection<GrantedAuthority> result = new HashSet<>();
		for (Role role : roles) {
			result.add(new SimpleGrantedAuthority(role.name));
		}
		return result;
	}
	public String getCompanyName() {
		if(departmentlongName.indexOf("/")==-1)
			return departmentlongName;
		else
		    return departmentlongName.substring(0, departmentlongName.indexOf("/"));
	}
	public String getDepartmentName() {
	    String result = departmentlongName.substring(getCompanyName().length());
	    if(result.startsWith("/")){
	    	result=result.substring(1);
	    }
	    return result;
	}
	public Department getDepartment() {
		return getDepartmentRepository().getDepartmentByLongName(departmentlongName);
	}

	private DepartmentRepository getDepartmentRepository() {
		if(this.departmentRepository==null){
			reloadTransientMenbers();
		}
		return this.departmentRepository;
	}
	private UserRoleRepository getUserRoleRepository() {
		if(this.userRoleRepository==null){
			reloadTransientMenbers();
		}
		return this.userRoleRepository;
	}
	public String getId() {
		return id;
	}

	public String getNote() {
		return note;
	}

	public String getOpenId() {
		return openId;
	}

	@Override
	public String getPassword() {
		return null;
	}

	public String getJobTitle() {
		return jobTitle;
	}

	@Override
	public String getUsername() {
		return this.loginName;
	}

	public String getChineseName() {
		return chineseName;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public boolean isInThisDepartment(String departmentlongName) {
		return this.departmentlongName.equals(departmentlongName);
	}

	public boolean isInThisOrChildDepartment(String departmentlongName) {
		return this.departmentlongName.contains(departmentlongName);
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public void setJobTitle(String jobTitle) {
		this.jobTitle = jobTitle;
	}

	@Override
	public void reloadTransientMenbers() {
		KingdeeUser temp=new KingdeeUser();
		this.departmentRepository=temp.departmentRepository;
		this.userRoleRepository=temp.userRoleRepository;
	}
	public String getImageUrl() {
		return imageUrl;
	}
	@Override
	public String getName() {
		return getUsername();
	}

}
